package be.nepherte.movmanager.client.ui.components;

import be.nepherte.movmanager.client.MovManager;
import be.nepherte.movmanager.domain.UserInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

/**
 * This class represents a login panel displaying the user's login status and
 * a link to the about dialog.
 *
 * <p>
 * Copyright © 2010 Bart Verhoeven
 * </p>
 * 
 * <p>
 * This file is part of MovManager.
 * </p>
 * 
 * <p>
 * MovManager is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * </p>
 * 
 * <p>
 * MovManager is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * </p>
 * 
 * <p>
 * You should have received a copy of the GNU General Public License along with
 * MovManager. If not, see http://www.gnu.org/licenses/.
 * </p>
 * 
 * @author	Bart Verhoeven
 * @version	0.1
 */
public class LoginPanel extends Composite implements ClickHandler {
	/**The login panel.*/
	private HorizontalPanel loginPanel = new HorizontalPanel();
	/**About dialog box with application info.*/
	private AboutDialogBox aboutDialog = new AboutDialogBox();
	/**Link to about panel.*/
	private Anchor about = new Anchor(MovManager.constants.linkAbout());
	/**
	 * Link with login status. Contains either a logout link or login link
	 * depending on the user's login status.
	 */
	private Anchor loginStatus = new Anchor();
	/**Placeholder for user's email address.*/
	private Label emailAddress = new Label();
	/**Placeholder for the email separator.*/
	private Label emailSeparator = new Label();
	/**Splash loading image.*/
	private Image loader = new Image(GWT.getModuleBaseURL() + "images/loader.gif");
	
	/**
	 * Creates a new login panel.
	 */
	public LoginPanel() {
		initLoginPanel();
		initWidget(loginPanel);
	}

	private void formatLoginPanel() {
		about.addClickHandler(this);
		emailAddress.setStyleName("boldStyle");
		loginPanel.setSpacing(5);
	}

	private void initLoginPanel() {
		formatLoginPanel();
		loginPanel.add(emailAddress);
		loginPanel.add(emailSeparator);
		loginPanel.add(loginStatus);
		loginPanel.add(new Label("|"));
		loginPanel.add(about);
	}
	
	public void setLoadingScreen(boolean enable) {
		if (enable && !loader.isAttached())
			loginPanel.insert(loader, 0);
		else
			loginPanel.remove(loader);
	}

	/**
	 * Sets the current login status based on the user info object. When the
	 * user is logged out, a link will be shown to log in with his Google
	 * Account. When the user is logged in, his google account is shown and a
	 * link will be shown to log out.
	 * 
	 * @param	info
	 *          the current user info
	 */
	public void setLoginStatus(UserInfo info) {
		if (info.isLoggedIn()) {
			emailAddress.setText(info.getEmailAddress());
			emailSeparator.setText("|");
			loginStatus.setText(MovManager.constants.linkLogout());
			loginStatus.setHref(info.getLogoutUrl());
		} else {
			emailAddress.setText("");
			emailSeparator.setText("");
			loginStatus.setText(MovManager.constants.linkLogin());
			loginStatus.setHref(info.getLoginUrl());
		}
	}

	/**
	 * Called when a native click event is fired by one of the clickable components
	 * in this login panel.
	 */
	@Override
	public void onClick(ClickEvent event) {
		if (event.getSource().equals(about))
			aboutDialog.center();
	}
}
